package com.qf.ems.entity;

/**
 * 1 在mysql当中，如何实现分页查询呢？
 *   limit 起始行,个数（页大小） ，例如：SELECT * FROM emp LIMIT 0,2 查第一页
 *   在固定页大小（页大小为2）的情况下，查第二页 ： SELECT * FROM emp LIMIT 2,2
 *   当前页数和起始行数有对应关系：起始行数=（当前页数-1）*页大小
 *
 *  2 注意：Page类的设计是OOP编程思想当中的，封装思想的例子
 *
 *
 */

public class Page {
    private Integer pageIndex;//当前页码

    private Integer pageSize;//页大小

    private Integer totalCounts;//数据总条数

    private Integer totalPages;//总页数

    private Integer startRows;//起始行

    @Override
    public String toString() {
        return "Page{" +
                "pageIndex=" + pageIndex +
                ", pageSize=" + pageSize +
                ", totalCounts=" + totalCounts +
                ", totalPages=" + totalPages +
                ", startRows=" + startRows +
                '}';
    }

    //单参构造函数，只指定当前显示第几页，页大小采用默认值
    public Page(Integer pageIndex) {
        this(pageIndex,2);
    }

    public Page(Integer pageIndex, Integer pageSize) {
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        this.setStartRows((pageIndex - 1) * pageSize);
    }

    public Integer getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalCounts() {
        return totalCounts;
    }

    public void setTotalCounts(Integer totalCounts) {
        this.totalCounts = totalCounts;
        //在赋值总条数之后，自动计算当前分页的总页数
        this.setTotalPages(this.totalCounts % this.pageSize ==0? this.totalCounts / this.pageSize : this.totalCounts / this.pageSize + 1);
    }

    public Integer getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(Integer totalPages) {
        this.totalPages = totalPages;
    }

    public Integer getStartRows() {
        return startRows;
    }

    public void setStartRows(Integer startRows) {
        this.startRows = startRows;
    }
}
